<?php
if ( ! defined('BASEPATH')) exit('No direct script access allowed');
/**
 *
 * 自主酒店后台
 * @author tk
 *
 */
class Hotel_Admin_module extends CI_Module {

    private $sid;
    /**
     *
     * 构造函数
     */
    public function __construct()
    {
        parent::__construct();
        $this->sid = $GLOBALS['sid'];
        $this->data = $GLOBALS['config'];
        $this->load->model('hotel.Hotel_model');
        $this->load->model('admin.area_model');
        $this->load->library('mypageclass');
    }





    public function index()
    {

        $where  = "sid = ".$this->sid  ;
        $condition['status'] = addslashes($this->input->get_post('status'));
        $condition['word'] = addslashes($this->input->get_post('word'));
        if($condition['word']){
            $where.= " AND (title LIKE '%".$condition['word']."%')";
        }
        if($condition['status']){
            $where .= " AND status = 0";
        }
        $page_config['perpage']=20;   //每页条数
        $page_config['part']=2;//当前页前后链接数量
        $page_config['url']='rent/admin/index';//url
        $page_config['param'] = '?word='.$condition['word'] ;//参数
        $page_config['seg']=4;//参数取 index.php之后的段数，默认为3，即index.php/control/function/18 这种形式
        $page_config['nowindex']=$this->uri->segment($page_config['seg']) ? $this->uri->segment($page_config['seg']):1;//当前页
        $page_config['total'] = $this->Hotel_model->get_hotel_count($where);
        $this->load->library('mypageclass');
        $this->mypageclass->initialize($page_config);
        $this->data['list'] = $this->Hotel_model->get_hotel_list($where,$page_config['perpage'],$page_config['nowindex']);

        $this->data['form_url'] = site_url('hotel/admin/index');
        $this->data['add_url'] = site_url('hotel/admin/add_hotel');
        $this->data['condition'] = $condition;
        $this->load->view('/admin/view_index',$this->data);
    }



    /**
     * 添加酒店
     *
     *
     */

    public function add_hotel()
    {

        if(strtolower($_SERVER['REQUEST_METHOD']) == 'post')
        {
            $array = $this->input->post('Form');
            $array['sid'] = $this->sid;
            $array['upts'] = time();
            $array['crts'] = time();
            $res = $this->Hotel_model->_add_hotel($array) ;
            $failure_url = site_url('hotel/admin/add_hotel');
            if($res)
            {
                $this->success('操作成功',site_url('hotel/admin/index'),true);
            }
            else
            {
                $this->error('操作失败',$failure_url,true);
            }
        }

        $this->data['form_url'] = site_url('hotel/admin/add_hotel/');
        $this->data['index_url'] = site_url('hotel/admin/index');
        $this->load->view('/admin/view_add_hotel',$this->data);
    }

    public function edit_hotel()
    {
        if(strtolower($_SERVER['REQUEST_METHOD']) == 'post')
        {
            $where = array('id'=>$this->input->get_post('id'));
            $data = $this->input->get_post('Form');
            $data['upts'] = time();
            $result = $this->Hotel_model->_edit_hotel($data,$where);
            if($result)
            {
                $this->success('操作成功',site_url('hotel/admin/index'),true);
            }
        }
        $id = $this->uri->segment(4);
        $this->data['info']= $this->Hotel_model->_hotel_info($id);
        $this->data['go_list']= $this->Hotel_model->get_price_list(array('hotel_id'=>$id));
        $this->data['hotel_id'] = $id ;
        $this->data['form_url'] = site_url('hotel/admin/edit_hotel');
        $this->data['index_url'] = site_url('hotel/admin/index');
        $this->data['ajax_add_price_url'] = site_url('hotel/admin/ajax_add_price?hotel_id='.$id);
         $this->load->view('admin/view_edit_hotel', $this->data);
    }


    public function ajax_add_price()
    {
        $this->data['hotel_id'] =  $this->input->get_post('hotel_id');
        $this->data['form_url'] = site_url('hotel/admin/ajax_add_price_save');
        $this->data['go_list']= $this->Hotel_model->get_price_list(array('hotel_id'=>$this->data['hotel_id']));
        $this->data['ajax_fast_url'] = site_url('hotel/admin/ajax_fast');
        $this->load->view('admin/ajax_add_price', $this->data);
    }


    public function ajax_add_price_save()
    {
        $param = $this->input->get_post('Form');
        $param['sid'] =  $this->sid;
        $condition = array(
            'go_date'=>$param['go_date'],
            'room_type'=>$param['room_type'],
            'hotel_id'=>$param['hotel_id'],
            'breakfast'=>$param['breakfast'],
            'network'=>$param['network'],
        );
        $verify = $this->Hotel_model->get_price_list($condition);
        if($verify)
        {
            $this->error('已存在,'.$param['go_date'].$this->Hotel_model->return_room_type($param['room_type']).'价格','',true);
        }
        if($this->Hotel_model->_add_price($param))
        {
            $this->success('添加价格成功',site_url('hotel/admin/edit_hotel/'.$param['hotel_id']),true);
        }

        $this->error('添加价格失败','',true);
    }


    /**
     * 酒店状态
     */

    public function hotel_status()
    {
        $post = $this->input->post();
        $data[$post['field']] = $post['val'];
        $res = $this->Hotel_model->_edit_hotel($data,array('id'=>$post['id']));
        if($res){
            $this->success('操作成功','',true);
        }else{
            $this->error('操作失败','',true);
        }
    }

    /**
     * 删除酒店
     * @param   POST 表单数据
     * return true/false
     */

    public function delete_hotel()
    {

        $id = $this->input->get('id');
        if(empty($id)){
            $post_id = $this->input->post('id');
            $res = $this->Hotel_model->_delete_hotel("id in ($post_id)");
        }else{

            $res = $this->Hotel_model->_delete_hotel(array('id'=>$id));
        }
        if($res){
            $this->success('操作成功',site_url('rent/admin/index'),true);
        }else{
            $this->error('操作失败','',true);
        }
    }

    /**
     * 删除酒店价格
     * @param   POST 表单数据
     * return true/false
     */

    public function delete_hotel_price()
    {
        $id = $this->input->get('id');
        $res = $this->Hotel_model->delete_hotel_price(array('id'=>$id));
        if($res){
            $this->success('操作成功','',true);
        }else{
            $this->error('操作失败','',true);
        }
    }




    /**
     * 附加产品列表
     *
     */

    public function product_index()
    {
        $where  = "sid = ".$this->sid  ;


        $condition['keywords'] = $this->input->get_post('keywords');
        $condition['type'] = strlen($this->input->get_post('type'))?$this->input->get_post('type'):'-1';


        if(strlen($condition['keywords']))
        {
            $where  .= " and name like '%".$condition['keywords']."%'"  ;
        }

        if($condition['type'] != '-1')
        {
            $where  .= " and type = ". $condition['type']  ;
        }


        $page_config['perpage']=20;   //每页条数
        $page_config['part']=2;//当前页前后链接数量
        $page_config['url']='hotel/admin/product_index';//url
        $page_config['param'] ='' ;//参数
        $page_config['seg']=4;//参数取 index.php之后的段数，默认为3，即index.php/control/function/18 这种形式
        $page_config['nowindex']=$this->uri->segment($page_config['seg']) ? $this->uri->segment($page_config['seg']):1;//当前页
        $page_config['total'] = $this->Hotel_model->get_extend_product_count($where);

        $this->mypageclass->initialize($page_config);
        $this->data['list'] = $this->Hotel_model->get_extend_product_list($where,$page_config['perpage'],$page_config['nowindex']);

        $this->data['form_url'] = site_url('hotel/admin/product_index');
        $this->data['add_url'] = site_url('hotel/admin/add_product');
        $this->data['condition'] = $condition;

        $this->load->view('/admin/product/index',$this->data);

    }



    /**
     * 附加产品列表
     *
     */

    public function add_product()
    {

        if(strtolower($_SERVER['REQUEST_METHOD']) == 'post')
        {
            $failure_url = site_url('hotel/admin/add_product');
            $form = $this->input->get_post('Form');

            $data = array(
                'sid'=>$this->sid,
                'name'=>$form['name'],
                'money'=>$form['money'],
                'crts'=>date('Y-m-d H:i:s')
            );

        
              $data['type'] =  $form['type'];

          

            if($form['id'])
            {
                $res = $this->Hotel_model->_edit_extend_product($data,array('id'=>$form['id'])) ;
            }
            else
            {

                $row =  $this->Hotel_model->_get_extend_product_row(array('name'=>$form['name'],'sid'=>$this->sid));
                $redirect_url = site_url('hotel/admin/edit_product/'.$row['id']);
                if($row) $this->error('已经有同款产品啦,正在跳转到编辑页面...',$redirect_url,true);

                $res = $this->Hotel_model->_add_extend_product($data) ;
            }


            if($res)
            {
                $this->success('操作成功',site_url('hotel/admin/product_index'),true);
            }
            else
            {
                $this->error('操作失败',$failure_url,true);
            }
        }
        $this->data['form_url'] = site_url('hotel/admin/add_product');
        $this->data['index_url'] = site_url('hotel/admin/product_index');
        $this->load->view('/admin/product/add',$this->data);
    }


    /**
     * 删除房间号
     * @param   POST 表单数据
     * return true/false
     */

    public function delete_product()
    {

        $id = $this->input->get('id');
        if(empty($id)){
            $post_id = $this->input->post('id');
            $res = $this->Hotel_model->_delete_extend_product("id in ($post_id)");
        }else{

            $res = $this->Hotel_model->_delete_extend_product(array('id'=>$id));
        }

        if($res){
            $this->success('操作成功',site_url('hotel/admin/code'),true);
        }else{
            $this->error('操作失败','',true);
        }
    }

    /**
     * 房间号编辑
     *
     */


    public function edit_product($id)
    {
        $this->data = $this->Hotel_model->_get_extend_product_row(array('id'=>$id));
        $this->data['edit_status'] = 1;
        $this->data['index_url'] = site_url('hotel/admin/product_index');
        $this->data['form_url'] = site_url('hotel/admin/add_product');
        $this->load->view('admin/product/add', $this->data);
    }



    /**
     * 房间号编辑
     *
     */


    public function code_edit($id)
    {
        $this->data = $this->Hotel_model->_get_number_row(array('id'=>$id));

        $this->data['list_url'] = site_url('hotel/admin/code');
        $this->data['form_url'] = site_url('hotel/admin/add_code');
        $this->load->view('admin/code/add', $this->data);
    }


    /**
     * 房间号列表
     * @param
     *
     */

    public function code()
    {

        $where = array('sid' => $this->sid);
        $page_config['perpage']=20;   //每页条数
        $page_config['part']=2;//当前页前后链接数量
        $page_config['url']='hotel/admin/code';//url
        $page_config['param'] = '' ;//参数
        $page_config['seg']=4;//参数取 index.php之后的段数，默认为3，即index.php/control/function/18 这种形式
        $page_config['nowindex']=$this->uri->segment($page_config['seg']) ? $this->uri->segment($page_config['seg']):1;//当前页
        $page_config['total'] = $this->Hotel_model->get_number_count($where);
        $this->load->library('mypageclass');
        $this->mypageclass->initialize($page_config);
        $this->data['list'] = $this->Hotel_model->get_number_list($where,$page_config['perpage'],$page_config['nowindex']);

        $this->data['add_url'] = site_url('hotel/admin/add_code');
        $this->load->view('admin/code/list', $this->data);
    }

    /**
     * 添加房间号列表
     * @param
     *
     */

    public function add_code()
    {
        if(strtolower($_SERVER['REQUEST_METHOD']) == 'post')
        {
            $form_data = $this->input->get_post('Form');

            $data['name'] = $form_data['name'];
            $data['number'] = $form_data['number'];
            $data['note'] = $form_data['note'];
            $data['status'] = $form_data['status'];
            $data['price'] = $form_data['price'];
            $data['is_suite'] = $form_data['is_suite'];
            $data['sid'] = $this->sid;


            if ($form_data['id']) {

                $data['upts'] =  date('Y-m-d H:i:s');
                $result = $this->Hotel_model->_edit_number($data,array('id'=>$form_data['id']));

            } else {

                $check = $this->Hotel_model->_get_number_row(array('name'=>$form_data['name'],'sid'=>$this->sid));
                if($check)  $this->error('房间号重复','',true);

                $data['upts'] =  $data['crts'] = date('Y-m-d H:i:s');
                $result = $this->Hotel_model->_add_number($data);
            }


            if($result)
            {
                $this->success('操作成功',site_url('hotel/admin/code'),true);
            }
        }

        $this->data['list_url'] = site_url('hotel/admin/code');
        $this->load->view('admin/code/add', $this->data);
    }


    /**
     * 删除房间号
     * @param   POST 表单数据
     * return true/false
     */

    public function delete_code()
    {

        $id = $this->input->get('id');
        if(empty($id)){
            $post_id = $this->input->post('id');
            $res = $this->Hotel_model->_delete_number("id in ($post_id)");
        }else{

            $res = $this->Hotel_model->_delete_number(array('id'=>$id));
        }

        if($res){
            $this->success('操作成功',site_url('hotel/admin/code'),true);
        }else{
            $this->error('操作失败','',true);
        }
    }

    /**
     * 房间状态
     */

    public function code_status()
    {
        $post = $this->input->post();
        $data[$post['field']] = $post['val'];
        $res = $this->Hotel_model->_edit_number($data,array('id'=>$post['id']));
        if($res){
            $this->success('操作成功','',true);
        }else{
            $this->error('操作失败','',true);
        }
    }






}